//
//  HotSellerTableViewController.m
//  GiftItems
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "HotSellerTableViewController.h"
#import "HotSellerTableViewCell.h"
#import "BlurImageView.h"
#import "HotSellerModel.h"
#import "HotSellerMoreCollectionViewController.h"
#import "WebViewController.h"
@interface HotSellerTableViewController ()
@property(nonatomic) BOOL isClick;
@property(nonatomic, strong) NSMutableArray *selModelArray;
@property(nonatomic, strong) NSString *nextUrl;
@property(nonatomic, strong) YYAnimationIndicator *indicator;
@property (nonatomic,strong)UIButton *toTopbutton;

@end

@implementation HotSellerTableViewController
// 解析数据
- (void)setUpDataWithUrl:(NSString *)url {
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *dataDic = dic[@"data"];
         self.nextUrl = dataDic[@"paging"][@"next_url"];
       // NSLog(@"%@",dataDic.allKeys);
        NSArray *array = dataDic[@"items"];
        for (NSDictionary *obj in array) {
           // NSLog(@"<><>%@<><>",obj.allKeys);
            HotSellerModel *model = [HotSellerModel shareJsonWithDictionary:obj[@"data"]];
            [self.selModelArray addObject:model];
        }
        [self StopBtn];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(NSMutableArray *)selModelArray {
    if (!_selModelArray) {
        _selModelArray = [[NSMutableArray alloc] init];
    }
    return _selModelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   [self setUpDataWithUrl:KHotSellerUrl];
    // 设置模糊视图
    self.tableView.backgroundView = [[BlurImageView alloc] initWithFrame:kBounds];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor orangeColor];
    self.tableView.rowHeight = 150;
    [self.tableView registerClass:[HotSellerTableViewCell class] forCellReuseIdentifier:@"hotCell"];
    [self addUIBarButtonItemButton];
   
    // 上拉加载数据
    __block HotSellerTableViewController *hotSelVC = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        [hotSelVC setUpDataWithUrl:hotSelVC.nextUrl];
        [hotSelVC.tableView.defaultFooter endRefreshing];
    }];
    // 下拉刷新数据
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        [hotSelVC.selModelArray removeAllObjects];
        [hotSelVC setUpDataWithUrl:KHotSellerUrl];
        [hotSelVC.tableView reloadData];
        [hotSelVC.tableView.defaultHeader endRefreshing];
    }];

    //添加加载动画
    self.indicator = [[YYAnimationIndicator alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-40, self.view.frame.size.height/2-40, 80, 80)];
    [self.indicator setLoadText:@"正在加载..."];
    [self.view addSubview:self.indicator];
    [self startBtn];
    
    [self addToTopButton];
 
    
}
// 回到最顶部
- (void)addToTopButton {
    self.toTopbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.toTopbutton.frame = CGRectMake(kWidth - 60, kHeight - 200, 40, 40);
    [self.toTopbutton setImage:[UIImage imageNamed:@"Entypo_e4af(0)_256"] forState:(UIControlStateNormal)];
    [self.toTopbutton addTarget:self action:@selector(toTop) forControlEvents:(UIControlEventTouchUpInside)];
    self.toTopbutton.alpha = 0;
    [self.view addSubview:self.toTopbutton];
}
- (void)toTop {
    
    self.tableView.contentOffset = CGPointMake(0, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    self.toTopbutton.alpha = self.tableView.contentOffset.y / 1000.0;
    self.toTopbutton.frame = CGRectMake(kWidth - 60, kHeight- 200 + self.tableView.contentOffset.y, 40, 40);
}
#pragma mark --- 加载动画 ---
- (void)startBtn {
    // 开始转动
    [self.indicator startAnimation];
}

/*
 当Type为YES时消失  NO时不消失
 */
-(void)StopBtn
{
    [self.indicator stopAnimationWithLoadText:@"finish" withType:YES];//加载成功
}

-(void)StopFailBtn
{
    [self.indicator stopAnimationWithLoadText:@"加载失败" withType:NO];//加载失败
}



// 布局模式转换
- (void)addUIBarButtonItemButton{
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Entypo_21c6(0)_48"] style:UIBarButtonItemStylePlain target:self action:@selector(moreSelectButton)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = leftButton;
    self.navigationController.navigationBar.tintColor = kRGB(255.0, 240.0, 255.0);
}

#pragma mark -- 点击按钮跳转下个界面 ---
// 点击更多按钮的点击方法
- (void)moreSelectButton {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [self setLayoutForCollectionView:layout];
    HotSellerMoreCollectionViewController *hotSellerVC = [[HotSellerMoreCollectionViewController alloc] initWithCollectionViewLayout:layout];
    [self.navigationController pushViewController:hotSellerVC animated:YES];
}

// 设置布局的样式
- (void)setLayoutForCollectionView:(UICollectionViewFlowLayout *)layout {
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(5, 11, 5, 11);
    layout.itemSize = CGSizeMake((kWidth - 55)/4, 100);
    layout.headerReferenceSize = CGSizeMake(kWidth, 30);
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     // Return the number of rows in the section.
    return self.selModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotSellerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotCell" forIndexPath:indexPath];
    HotSellerModel *model = self.selModelArray[indexPath.row];
    [cell setValueForAttributesWithModel:model];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HotSellerModel *model = self.selModelArray[indexPath.row];
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.webUrl = model.purchase_url;
    webVC.nameShare = model.name;
    webVC.imageShare = model.cover_image_url;
    webVC.type = @"热卖";
    [self presentViewController:webVC animated:YES completion:nil];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
